package com.example.doublepink.networktest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private EditText mTxtUrl;
    private Button mBtnSend;
    private TextView mTxtShow,mTxtErrShow;
    private final OkHttpClient client = new OkHttpClient();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTxtShow = (TextView)findViewById(R.id.txtShow);
        mTxtUrl = (EditText)findViewById(R.id.txtUrlInput);
        mTxtErrShow = (TextView)findViewById(R.id.txtErrShow);
        mTxtUrl.setText("http://119.29.209.198:8080/webside-app/tmUser/getAuthCode.json");

        mBtnSend =(Button)findViewById(R.id.btn);
        mBtnSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sqlCon();
            }
        });

    }


    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    String digit = "5";

    private void sqlCon(){

        mTxtShow.setText("");
        mTxtErrShow.setText("");





        if ("".equals(mTxtUrl.getText().toString().trim())){
            Toast.makeText(this.getApplicationContext(), "请设置网络连接地址", Toast.LENGTH_SHORT)
                    .show();
            return;
        }


        JSONObject jsonParam = new JSONObject();//定义json对象
        try {
            jsonParam.put("digit", digit);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String path = mTxtUrl.getText().toString().trim(); //组合请求地址
        RequestBody body = RequestBody.create(JSON, String.valueOf(jsonParam));
        Request request = new Request.Builder()
                .url(path)
                .post(body)
                .build();

        Call call = client.newCall(request);


        // 开启异步线程访问网络
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Message msg = new Message();
                msg.what = 0;
                handler.sendMessage(msg);
            }

            @Override
            public void onResponse(Response response) throws IOException {
               String results = response.body().string();


                Message msg = new Message();
                msg.obj = results;
                if (null!= results){
                    msg.what = 1;
                }else {
                    msg.what = 0;
                }
                handler.sendMessage(msg);

            }
        });
    }
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1: //成功
                    String str = String.valueOf(msg.obj);
                    mTxtShow.setText(str);

                    break;
                case 0: //失败
                    String strErr = String.valueOf(msg.obj);
                    mTxtErrShow.setText(strErr);

                    break;
                default:
                    break;
            }
        }
    };

}
